{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'pandas.core.frame.DataFrame'>\n",
      "RangeIndex: 5395 entries, 0 to 5394\n",
      "Data columns (total 10 columns):\n",
      " #   Column        Non-Null Count  Dtype \n",
      "---  ------        --------------  ----- \n",
      " 0   ts_code       5395 non-null   object\n",
      " 1   symbol        5395 non-null   object\n",
      " 2   name          5395 non-null   object\n",
      " 3   area          5380 non-null   object\n",
      " 4   industry      5380 non-null   object\n",
      " 5   cnspell       5395 non-null   object\n",
      " 6   market        5395 non-null   object\n",
      " 7   list_date     5395 non-null   object\n",
      " 8   act_name      2575 non-null   object\n",
      " 9   act_ent_type  2575 non-null   object\n",
      "dtypes: object(10)\n",
      "memory usage: 421.6+ KB\n",
      "None\n",
      "ts_code         5395\n",
      "symbol          5395\n",
      "name            5395\n",
      "area            5380\n",
      "industry        5380\n",
      "cnspell         5395\n",
      "market          5395\n",
      "list_date       5395\n",
      "act_name        2575\n",
      "act_ent_type    2575\n",
      "dtype: int64\n",
      "股票基本信息已成功写入数据库。\n",
      "1, 000001.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000001.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000001.SZ 每日数据已成功写入数据库。\n",
      "2, 000002.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000002.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000002.SZ 每日数据已成功写入数据库。\n",
      "3, 000004.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000004.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000004.SZ 每日数据已成功写入数据库。\n",
      "4, 000006.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000006.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000006.SZ 每日数据已成功写入数据库。\n",
      "5, 000007.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000007.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000007.SZ 每日数据已成功写入数据库。\n",
      "6, 000008.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000008.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000008.SZ 每日数据已成功写入数据库。\n",
      "7, 000009.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000009.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000009.SZ 每日数据已成功写入数据库。\n",
      "8, 000010.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000010.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000010.SZ 每日数据已成功写入数据库。\n",
      "9, 000011.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000011.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000011.SZ 每日数据已成功写入数据库。\n",
      "10, 000012.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000012.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000012.SZ 每日数据已成功写入数据库。\n",
      "11, 000014.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000014.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000014.SZ 每日数据已成功写入数据库。\n",
      "12, 000016.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000016.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000016.SZ 每日数据已成功写入数据库。\n",
      "13, 000017.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000017.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000017.SZ 每日数据已成功写入数据库。\n",
      "14, 000019.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000019.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000019.SZ 每日数据已成功写入数据库。\n",
      "15, 000020.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000020.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000020.SZ 每日数据已成功写入数据库。\n",
      "16, 000021.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000021.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000021.SZ 每日数据已成功写入数据库。\n",
      "17, 000025.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000025.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000025.SZ 每日数据已成功写入数据库。\n",
      "18, 000026.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000026.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000026.SZ 每日数据已成功写入数据库。\n",
      "19, 000027.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000027.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000027.SZ 每日数据已成功写入数据库。\n",
      "20, 000028.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000028.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000028.SZ 每日数据已成功写入数据库。\n",
      "21, 000029.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000029.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000029.SZ 每日数据已成功写入数据库。\n",
      "22, 000030.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000030.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000030.SZ 每日数据已成功写入数据库。\n",
      "23, 000031.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000031.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000031.SZ 每日数据已成功写入数据库。\n",
      "24, 000032.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000032.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000032.SZ 每日数据已成功写入数据库。\n",
      "25, 000034.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000034.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000034.SZ 每日数据已成功写入数据库。\n",
      "26, 000035.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000035.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000035.SZ 每日数据已成功写入数据库。\n",
      "27, 000036.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000036.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000036.SZ 每日数据已成功写入数据库。\n",
      "28, 000037.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000037.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000037.SZ 每日数据已成功写入数据库。\n",
      "29, 000039.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000039.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000039.SZ 每日数据已成功写入数据库。\n",
      "30, 000040.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000040.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000040.SZ 每日数据已成功写入数据库。\n",
      "31, 000042.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000042.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000042.SZ 每日数据已成功写入数据库。\n",
      "32, 000045.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000045.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000045.SZ 每日数据已成功写入数据库。\n",
      "33, 000048.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000048.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000048.SZ 每日数据已成功写入数据库。\n",
      "34, 000049.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000049.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000049.SZ 每日数据已成功写入数据库。\n",
      "35, 000050.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000050.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000050.SZ 每日数据已成功写入数据库。\n",
      "36, 000055.SZ\n"
     ]
    },
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\史昊轩\\AppData\\Local\\Temp\\ipykernel_17368\\4151655877.py:44: UserWarning: The provided table name '000055.SZ' is not found exactly as such in the database after writing the table, possibly due to case sensitivity issues. Consider using lower case table names.\n",
      "  stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "000055.SZ 每日数据已成功写入数据库。\n",
      "stock_basic 表中的记录数:\n",
      "   COUNT(*)\n",
      "0      5395\n",
      "中国石油的信息:\n",
      "     ts_code  symbol  name area industry cnspell market list_date act_name  \\\n",
      "0  601857.SH  601857  中国石油   北京     石油开采    zgsy     主板  20071105     None   \n",
      "\n",
      "  act_ent_type  \n",
      "0         None  \n",
      "000009.sz 的信息:\n",
      "         ts_code trade_date  open  high   low  close  pre_close  change  \\\n",
      "0      000009.SZ   20250124  8.54  8.63  8.51   8.54       8.53    0.01   \n",
      "1      000009.SZ   20250123  8.65  8.77  8.52   8.53       8.56   -0.03   \n",
      "2      000009.SZ   20250122  8.64  8.64  8.46   8.56       8.64   -0.08   \n",
      "3      000009.SZ   20250121  8.80  8.83  8.60   8.64       8.72   -0.08   \n",
      "4      000009.SZ   20250120  8.71  8.85  8.67   8.72       8.66    0.06   \n",
      "...          ...        ...   ...   ...   ...    ...        ...     ...   \n",
      "17995  000009.SZ   19990623  4.10  4.25  4.10   4.17       4.10    0.07   \n",
      "17996  000009.SZ   19990622  4.02  4.10  3.95   4.10       4.00    0.10   \n",
      "17997  000009.SZ   19990621  3.90  4.03  3.83   4.00       3.90    0.10   \n",
      "17998  000009.SZ   19990618  3.96  3.97  3.79   3.90       3.99   -0.09   \n",
      "17999  000009.SZ   19990617  4.12  4.15  3.92   3.99       4.12   -0.13   \n",
      "\n",
      "       pct_chg        vol       amount  \n",
      "0       0.1172  118421.43  101515.6260  \n",
      "1      -0.3505  129813.41  112114.1810  \n",
      "2      -0.9259  103666.08   88498.8640  \n",
      "3      -0.9174   85851.74   74322.3370  \n",
      "4       0.6928  107580.98   93976.1900  \n",
      "...        ...        ...          ...  \n",
      "17995   1.7100  142631.00   59295.0560  \n",
      "17996   2.5000  114696.00   46251.6705  \n",
      "17997   2.5600  120029.00   47449.0672  \n",
      "17998  -2.2600  111478.00   42878.8067  \n",
      "17999  -3.1600  124205.00   49311.2962  \n",
      "\n",
      "[18000 rows x 11 columns]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import pandas as pd\n",
    "import tushare as ts\n",
    "from sqlalchemy import create_engine\n",
    "import matplotlib\n",
    "matplotlib.use(\"TkAgg\")\n",
    "import matplotlib.pyplot as plt\n",
    "from matplotlib.collections import LineCollection\n",
    "from sklearn import cluster, covariance, manifold\n",
    "from matplotlib.font_manager import FontProperties\n",
    "\n",
    "# 初始化 Tushare API\n",
    "pro = ts.pro_api('eb40c02798b5f92812c3bcc2cc837933c66ba60d3f63dfc1772b45ce')\n",
    "\n",
    "try:\n",
    "    # 获取股票基本信息\n",
    "    data = pro.query('stock_basic', exchange='', list_status='L')\n",
    "    print(data.info())\n",
    "    print(data.count())\n",
    "\n",
    "    # 创建数据库引擎\n",
    "    engine = create_engine('mysql+pymysql://root:shx20050507@localhost/stock_basic')\n",
    "\n",
    "    # 将股票基本信息写入数据库\n",
    "    data.to_sql('stock_basic', con=engine, if_exists='replace', index=False)\n",
    "    print(\"股票基本信息已成功写入数据库。\")\n",
    "\n",
    "    # 获取股票列表\n",
    "    stock_list = data['ts_code'].tolist()\n",
    "\n",
    "    # 存储每日价格数据\n",
    "    data_price = []\n",
    "\n",
    "    i = 0\n",
    "    for stock in stock_list:\n",
    "        if i == 36:\n",
    "            break\n",
    "        i += 1\n",
    "        print(f\"{i}, {stock}\")\n",
    "        try:\n",
    "            # 获取股票每日数据\n",
    "            stock_data = pro.daily(ts_code=stock)\n",
    "            # 将数据写入数据库\n",
    "            stock_data.to_sql(stock, con=engine, if_exists='append', index=False)\n",
    "            print(f\"{stock} 每日数据已成功写入数据库。\")\n",
    "        except Exception as e:\n",
    "            print(f\"写入 {stock} 数据时出错: {e}\")\n",
    "\n",
    "    # 查询数据库\n",
    "    sql = \"SELECT COUNT(*) FROM stock_basic\"\n",
    "    df = pd.read_sql(sql, engine)\n",
    "    print(\"stock_basic 表中的记录数:\")\n",
    "    print(df)\n",
    "\n",
    "    sql = \"SELECT * FROM stock_basic WHERE name='中国石油'\"\n",
    "    df = pd.read_sql(sql, engine)\n",
    "    print(\"中国石油的信息:\")\n",
    "    print(df)\n",
    "\n",
    "    sql = \"SELECT * FROM `000009.sz`\"\n",
    "    df = pd.read_sql(sql, engine)\n",
    "    print(\"000009.sz 的信息:\")\n",
    "    print(df)\n",
    "\n",
    "except Exception as e:\n",
    "    print(f\"程序运行出错: {e}\")\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "tensorflow",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.16"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
